package com.star.wellconnect.appts;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.star.wellconnect.R;
import com.star.wellconnect.sqlite.AppPreferences;
import com.star.wellconnect.sqlite.Appointment;
import com.star.wellconnect.sqlite.Patient;

public class ApptFragment extends Fragment{
    List<String> mApptsDates;
    Map<String, List<Appointment>> mApptsCollection;
	private ExpandableListView mApptsList;
	private ApptsListViewAdapter mAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		return inflater.inflate(R.layout.fragment_appt, container, false);
	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState){
		super.onViewCreated(view, savedInstanceState);
		// Retrieve data from database
		Patient patient = AppPreferences.getPatient(getActivity());
		List<Appointment> appts = AppPreferences.getAppointments(getActivity(), patient.username);
		
		// Initialize Helper
		DateFormat df = DateFormat.getDateInstance();
		df.setTimeZone(TimeZone.getDefault());
		LayoutInflater inflater = this.getLayoutInflater(savedInstanceState);
		
		// Retrieve Layout Elements
		mApptsList = (ExpandableListView) view.findViewById(R.id.appts_lists);
		
		// Create & Set Header
		((TextView) view.findViewById(R.id.today_date)).setText(df.format(new Date()));
		TextView headerAddNew = (TextView) view.findViewById(R.id.header_add_new);
		headerAddNew.setClickable(true);
		
		// Create & Set Footer
		ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.listview_footer_appt, mApptsList, false);
		TextView footerAddNew = (TextView) footer.findViewById(R.id.footer_add_new);
		footerAddNew.setClickable(true);
		mApptsList.addFooterView(footer);
		
		// Set Adapter
		parseDates(appts);
		mAdapter = new ApptsListViewAdapter(getActivity().getApplicationContext(), mApptsDates, mApptsCollection);
		mApptsList.setAdapter(mAdapter);
		
		// Set Listener
		headerAddNew.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ApptActivity.class);
                startActivity(intent);
			}
		});
		footerAddNew.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ApptActivity.class);
                startActivity(intent);
			}
		});
	}
	
	private void parseDates(List<Appointment> appts) {
		mApptsDates = new ArrayList<String>();
        mApptsCollection = new LinkedHashMap<String, List<Appointment>>();
        List<Appointment> tempList;
		for(Appointment appt: appts){
			String date = appt.getDate(AppPreferences.DOW_MIY_DATE);
			if(!mApptsDates.contains(date))
				mApptsDates.add(date);
			if(mApptsCollection.containsKey(date)){
				tempList = mApptsCollection.get(date);
				tempList.add(appt);
			}else{
				tempList = new ArrayList<Appointment>();
				tempList.add(appt);
			}
			mApptsCollection.put(date, tempList);
		}
    }
	
	@Override
	public void onStart(){
		mAdapter.notifyDataSetChanged();
		mAdapter.notifyDataSetInvalidated();
		mApptsList.invalidate();
		super.onStart();
	}
	
	@Override
	public void onResume(){
		mAdapter.notifyDataSetChanged();
		mAdapter.notifyDataSetInvalidated();
		mApptsList.invalidate();
		super.onResume();
	}

    // Convert pixel to dip
	public int getDipsFromPixel(float pixels) {
		// Get the screen's density scale
		final float scale = getResources().getDisplayMetrics().density;
		// Convert the dps to pixels, based on density scale
		return (int) (pixels * scale + 0.5f);
	}
}
